import requests
from bs4 import BeautifulSoup

# 视频列表: 白鼠马保国鬼畜、马保国武德经典、马保国闪电鞭等视频的弹幕
cidlist = ['140610898', '218895837', '104871439', '193758186', '254420722', '248312882', '249109762', '248498006',
           '248540098', '228247939', '253608078', '253029703', '253810389', '203172009', '192207926', '193876063',
           '253205607', '252003516', '192795872', '254947843']
bullets = []
for cid in cidlist:
    url = 'https://comment.bilibili.com/' + cid + '.xml'  # 'https://comment.bilibili.com/252003516.xml'
    request = requests.get(url)  # 获取页面
    request.encoding = 'utf-8'  # 因为是中文，我们需要进行转码，否则出来的都是unicode
    soup = BeautifulSoup(request.text, 'lxml')
    results = soup.find_all('d')  # 找出所有'd'
    comments = [comment.text for comment in results]  # 因为出来的时候是bs4格式的，我们需要把他转化成list
    bullets += comments

# 写入txt
with open('test.txt', 'w+', encoding="utf-8") as f:
    for ctext in bullets:
        f.writelines(ctext + "、")
